package com.haifeng.lyw.blerobot.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioGroup;

import com.haifeng.lyw.blerobot.R;
import com.haifeng.lyw.blerobot.utils.L;


public class ChassisDialog implements View.OnTouchListener, DialogInterface.OnDismissListener {
    private Context mContext;
    private String mtitle;
    private ClickListenerInterface clickListenerInterface;
    private Dialog dialog;
    private View customview;
    private RadioGroup addLightGroup;

    String TAG = "";
    
    Button mForearmUp, mForearmDown, mchassisfront, mchassisBack, mchassisLeft, mchassisRight, mPostUp, mPostDown;
    /**
     * Dialog主题
     */
    private int theme;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                L.e(TAG, "dialog--按钮按下");
                if (clickListenerInterface != null)
                    clickListenerInterface.send(v.getId(), true);
                break;
            case MotionEvent.ACTION_UP:
                L.e(TAG, "dialog--按钮抬起");
                if (clickListenerInterface != null)
                    clickListenerInterface.send(v.getId(), false);
                break;
        }
        return true;
    }

    public interface ClickListenerInterface {
        void send(int id, boolean action);
        void dissmis();
    }
    public ChassisDialog(Context context, String title, int theme) {
        mContext=context;
        mtitle=title;
        this.theme = theme;
        this.theme = R.style.NewSettingDialog;
    }

    public void init(){
        dialog = new Dialog(mContext,theme);
        customview = LayoutInflater.from(mContext).inflate(R.layout.dialog_chassis_layout, null);
        dialog.setContentView(customview);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setOnDismissListener(this);

        DisplayMetrics m = mContext.getResources().getDisplayMetrics();
        int screenWidth = Math.min(m.widthPixels, m.heightPixels);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (screenWidth * 1.3);
        params.height = (int) (screenWidth * 0.8);
        dialog.getWindow().setAttributes(params);
        initView();
    }

    private void initView(){
        mForearmUp = (Button) customview.findViewById(R.id.forearm_up);
        mForearmUp.setOnTouchListener(this);
        mForearmDown = (Button) customview.findViewById(R.id.forearm_down);
        mForearmDown.setOnTouchListener(this);
        mchassisfront = (Button) customview.findViewById(R.id.chassis_front);
        mchassisfront.setOnTouchListener(this);
        mchassisBack = (Button) customview.findViewById(R.id.chassis_back);
        mchassisBack.setOnTouchListener(this);
        mchassisLeft = (Button) customview.findViewById(R.id.chassis_left);
        mchassisLeft.setOnTouchListener(this);
        mchassisRight = (Button) customview.findViewById(R.id.chassis_right);
        mchassisRight.setOnTouchListener(this);
        mPostUp = (Button) customview.findViewById(R.id.postbrachium_up);
        mPostUp.setOnTouchListener(this);
        mPostDown = (Button) customview.findViewById(R.id.postbrachium_down);
        mPostDown.setOnTouchListener(this);
    }
    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }


    /**
     * 显示
     */
    public void show() {
        if (dialog == null){
            init();
        }
        dialog.show();
        fullScreen();
    }
    /**
     * 是否显示
     *
     * @return 是否显示
     */
    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    /**
     * dialog 小时
     */
    public void dismiss() {
        if (isShowing()) {
            dialog.dismiss();

        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (clickListenerInterface != null)
            clickListenerInterface.dissmis();
    }
    /**
     * 全屏显示方法
     */
    public void fullScreen() {
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN; // hide status bar

        if (android.os.Build.VERSION.SDK_INT >= 19) {
            uiFlags |= 0x00001000;    //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
        } else {
            uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }
        ((Activity)mContext).getWindow().getDecorView().setSystemUiVisibility(uiFlags);
    }
}
